package com.example.shangjinlieren.fractionPage;

import com.example.shangjinlieren.ResourceTable;
import com.example.shangjinlieren.entity.moneyList.OfferAReward;
import com.example.shangjinlieren.entity.moneyList.Tab;
import com.example.shangjinlieren.model.moneyList.OfferARewardModel;
import com.example.shangjinlieren.model.moneyList.Tabsmodel;
import com.example.shangjinlieren.provider.MoneyListProvider;
import com.example.shangjinlieren.slice.MainMoneyListDetailSlice;
import com.example.shangjinlieren.tabbar.fraction.BaseFraction;
import com.example.shangjinlieren.theme.MyColors;
import com.example.shangjinlieren.utils.RpxUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.app.Context;

import java.util.List;

public class MoneyList  extends BaseFraction {

    private Context context;
    private AbilitySlice abilitySlice;
    private DirectionalLayout component;//当前fraction 页面体
    private LayoutScatter scatter;
    private ComponentContainer container;
    private Intent intent;
    private DirectionalLayout body;
    public MoneyList(AbilitySlice abilitySlice) {
        this.abilitySlice = abilitySlice;

    }
    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        //获取fraction 体
        this.scatter=scatter;
        this.container = container;
        this.intent =intent;
        this.context = container.getContext();
        this.component =(DirectionalLayout)scatter.parse(ResourceTable.Layout_money_list, container, false);
        return component;
    }


    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        initPage();
    }
    protected void initPage(){
         body =(DirectionalLayout) component.findComponentById(ResourceTable.Id_top_search);
//        body.setHeight((int) RpxUtil.rpx(94,context)+(int) RpxUtil.vp2px(42,this.context));
        body.setPaddingTop((int) RpxUtil.vp2px(44,this.context));

        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(MyColors.themeColorGb);
        body.setBackground(shapeElement);
        initSearch();
        initlist();
    }

    protected void initSearch(){
        Component Layout_search = this.scatter.parse(ResourceTable.Layout_moneyListSearch, container, false);
        Layout_search.setHeight((int) RpxUtil.rpx(94,context) );
        Image Id_search_userlogin =(Image) Layout_search.findComponentById(ResourceTable.Id_search_userlogin);//登录
        Id_search_userlogin.setWidth((int) RpxUtil.rpx(73,context));
        Id_search_userlogin.setHeight((int) RpxUtil.rpx(73,context));
        Id_search_userlogin.setMarginLeft((int) RpxUtil.rpx(25,context));
        Id_search_userlogin.setScaleMode(Image.ScaleMode.INSIDE);
        StackLayout Id_search_message_item =(StackLayout) Layout_search.findComponentById(ResourceTable.Id_search_message_item);//搜索框
        Id_search_message_item.setWidth((int) RpxUtil.rpx(53,context));
        Id_search_message_item.setHeight((int) RpxUtil.rpx(53,context));
        Id_search_message_item.setMarginRight((int) RpxUtil.rpx(25,context));
        Text Id_search_message_nums =(Text) Layout_search.findComponentById(ResourceTable.Id_search_message_nums);//消息
        ShapeElement Id_search_message_numsElement = new ShapeElement();
        RgbColor rgbColor = new RgbColor(253,62,59);
        Id_search_message_numsElement.setRgbColor(rgbColor);
        Id_search_message_numsElement.setCornerRadius((int) RpxUtil.rpx(25,context));
        Id_search_message_nums.setWidth((int) RpxUtil.rpx(25,context));
        Id_search_message_nums.setHeight((int) RpxUtil.rpx(25,context));
        Id_search_message_nums.setBackground(Id_search_message_numsElement);

        Image Id_search_message_img =(Image) Layout_search.findComponentById(ResourceTable.Id_search_message_img);//消息
        Id_search_message_img.setWidth((int) RpxUtil.rpx(48,context));
        Id_search_message_img.setHeight((int) RpxUtil.rpx(48,context));
        Id_search_message_img.setScaleMode(Image.ScaleMode.INSIDE);
        Image Id_search_fangda =(Image) Layout_search.findComponentById(ResourceTable.Id_search_input_img); //搜索框
        Id_search_fangda.setWidth((int) RpxUtil.rpx(486,context));
        Id_search_fangda.setHeight((int) RpxUtil.rpx(56,context));
        Id_search_fangda.setScaleMode(Image.ScaleMode.INSIDE);

        this.body.addComponent(Layout_search);
        /**
         * tabs
         */
        Component Layout_moneyListTabs = this.scatter.parse(ResourceTable.Layout_moneyListTabs, container, false);
        TabList tabList = (TabList) Layout_moneyListTabs.findComponentById(ResourceTable.Id_tab_list);
        tabList.setTabTextSize((int) RpxUtil.rpx(30,context));

        tabList.setSelectedTabIndicatorColor(MyColors.themeColor);
        tabList.setSelectedTabIndicatorHeight((int) RpxUtil.rpx(6,context));
        tabList.setIndicatorType(TabList.INDICATOR_BOTTOM_LINE);
        tabList.setTabMargin((int) RpxUtil.rpx(30,context));//tab之间距离
        tabList.setHeight((int) RpxUtil.rpx(80,context));
        tabList.setTabTextColors(MyColors.secondColor,MyColors.themeColor);//设置文字颜色
        Tabsmodel tabsmodel = new Tabsmodel();
        List<Tab> tabs = tabsmodel.getTabs();
        tabs.forEach(t->{
            TabList.Tab tab = tabList.new Tab(context);
            tab.setText(t.getText());
            tabList.addTab(tab);
        });
        tabList.getTabAt(0).select();//默认选择推荐
        /**
         * 监听点击事件
         */
        tabList.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                // 当某个Tab从未选中状态变为选中状态时的回调
                System.out.println("选中了");
                TableLayout   Id_list_moneylistbody =(TableLayout) component.findComponentById(ResourceTable.Id_list_moneylistbody);
                Id_list_moneylistbody.removeAllComponents();
                OfferARewardModel offerARewardModel = new OfferARewardModel();
                List<OfferAReward> offerARewards = offerARewardModel.getOfferARewards();
                if(offerARewards.size()%2==0){
                    Id_list_moneylistbody.setRowCount(offerARewards.size()/2);
                }else {
                    double ceil = Math.ceil(offerARewards.size() / 2);
                    System.out.println("ceil======="+ceil);
                    Id_list_moneylistbody.setRowCount((int) ceil);
                }
                MoneyListProvider moneyListProvider = new MoneyListProvider(offerARewards, abilitySlice);
                List<DirectionalLayout> component = moneyListProvider.getComponent();
                for (int i = 0; i <3 ; i++) {
                    Id_list_moneylistbody.addComponent(component.get(i));
                }
                Id_list_moneylistbody.setAlignmentType(TableLayout.ALIGN_CONTENTS);

            }


            @Override
            public void onUnselected(TabList.Tab tab) {
                // 当某个Tab从选中状态变为未选中状态时的回调
                System.out.println("取消了");
            }


            @Override
            public void onReselected(TabList.Tab tab) {
                // 当某个Tab已处于选中状态，再次被点击时的状态回调
                System.out.println("取消了");
            }
        });
        this.body.addComponent(Layout_moneyListTabs);
    }
    protected void initlist(){
        TableLayout   Id_list_moneylistbody =(TableLayout) component.findComponentById(ResourceTable.Id_list_moneylistbody);
        OfferARewardModel offerARewardModel = new OfferARewardModel();
        List<OfferAReward> offerARewards = offerARewardModel.getOfferARewards();
        if(offerARewards.size()%2==0){
            Id_list_moneylistbody.setRowCount(offerARewards.size()/2);
        }else {
            double ceil = Math.ceil(offerARewards.size() / 2);
            System.out.println("ceil======="+ceil);
            Id_list_moneylistbody.setRowCount((int) ceil);
        }
        MoneyListProvider moneyListProvider = new MoneyListProvider(offerARewards, abilitySlice);
        List<DirectionalLayout> component = moneyListProvider.getComponent();
        for (int i = 0; i <component.size() ; i++) {
            Id_list_moneylistbody.addComponent(component.get(i));
            DirectionalLayout directionalLayout = component.get(i);
            directionalLayout.setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    abilitySlice.present(new MainMoneyListDetailSlice(),intent);
                }
            });
        }
        Id_list_moneylistbody.setAlignmentType(TableLayout.ALIGN_CONTENTS);

    }


    @Override
    protected void onActive() {
        super.onActive();
    }

    @Override
    protected void onForeground(Intent intent) {
        System.out.println("MoneyList+onForeground");
        super.onForeground(intent);
    }

    @Override
    protected void onInactive() {
        super.onInactive();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public int getUIContent() {
        return ResourceTable.Layout_money_list;
    }

    @Override
    public void initComponent() {

    }
}
